package com.weiwu.weiwustore.controller;


import com.weiwu.weiwustore.pojo.ApplyListVo;
import com.weiwu.weiwustore.pojo.PageVo;
import com.weiwu.weiwustore.pojo.RestResponse;
import com.weiwu.weiwustore.pojo.TaskListVo;
import com.weiwu.weiwustore.service.ApplyService;
import com.weiwu.weiwustore.utils.CrudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 申请单相关接口
 */
@RestController
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    /**
     * 我的申请
     * @param map 搜索条件
     * @return 相关申请单表格数据
     */
    @PostMapping("/apply/getapplys")
    public RestResponse getapplys(@RequestBody Map<String,String> map) {
        // 判断必要参数是否满足
        if(!CheckParames(map))
            return CrudUtil.ID_MISS_RESPONSE;
        PageVo<ApplyListVo> res = applyService.getapplysByUser(map);
        return new RestResponse(res.getRes(), res.getCount(), 200);
    }

    /**
     * 我的申请 - 详情
     * @param map 申请单号
     * @return 申请单详情
     */
    @PostMapping("/apply/applydetails")
    public RestResponse applydetails(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if (String.valueOf(map.get("applicationid")) == null) {
            return CrudUtil.ID_MISS_RESPONSE;
        }
        return new RestResponse(applyService.applydetails(map));
    }

    /**
     * 我的申请 - 催单
     * @param map 用户id和申请单id
     * @return 是否催单成功
     */
    @PostMapping("/apply/urgeThis")
    public RestResponse urgeThis(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if (String.valueOf(map.get("applicationid")) == null) {
            return CrudUtil.ID_MISS_RESPONSE;
        }
        return applyService.urge(map)==200 ? new RestResponse(200) : CrudUtil.RE_URGE_RESPONSE;
    }

    /**
     * 我的任务
     * @param map 搜索条件
     * @return 相关申请单表格数据
     */
    @PostMapping("/task/getmytask")
    public RestResponse getmytask(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if(!CheckParames(map))
            return CrudUtil.ID_MISS_RESPONSE;
        PageVo<TaskListVo> res = applyService.gettasksByUser(map);
        return new RestResponse(res.getRes(), res.getCount(), 200);
    }

    /**
     * 我的任务 - 审核或验收
     */
    @PostMapping("/task/examine")
    public RestResponse examine(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if(!CheckParames(map))
            return CrudUtil.ID_MISS_RESPONSE;
        Integer res = applyService.examine(map);
        // 判断此申请单是否需要当前用户审核验收
        if(res==401)
            return CrudUtil.NOT_EXAMINE_RESPONSE;
        return res==200 ? new RestResponse(200) : CrudUtil.EXAMINE_RESPONSE;
    }



    /**
     * 获取申请单状态selectItem数据
     * @return
     */
    @GetMapping("/apply/getstateitem")
    public RestResponse getstateitem() {
        return new RestResponse(applyService.getStateItem());
    }

    /**
     * 获取入库单数据
     * @param map 查询条件
     * @return
     */
    @PostMapping("/apply/getinrecord")
    public RestResponse getinrecord(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if (String.valueOf(map.get("id")) == null || String.valueOf(map.get("email")) == null) {
            return CrudUtil.ID_MISS_RESPONSE;
        }
        PageVo<ApplyListVo> res = applyService.getapplysByUserToMG(map);
        return new RestResponse(res.getRes(), res.getCount(), 200);
    }

    /**
     * 获取出库单数据
     * @param map 查询条件
     * @return
     */
    @PostMapping("/apply/getoutrecord")
    public RestResponse getoutrecord(@RequestBody Map<String,String> map) {
        //判断必要参数是否满足
        if (String.valueOf(map.get("id")) == null || String.valueOf(map.get("email")) == null) {
            return CrudUtil.ID_MISS_RESPONSE;
        }
        PageVo<ApplyListVo> res = applyService.getapplysByUserToMG(map);
        return new RestResponse(res.getRes(), res.getCount(), 200);
    }
    /**
     * 入库申请提交
     * @param map 用户对象，申请单主体，材料清单，tag组
     * @return
     */
    @PostMapping("/apply/commitapply")
    public RestResponse commitApply(@RequestBody Map<String,Object> map) {
        return CrudUtil.postHandle(applyService.commitApply(map), 1);
    }

    private boolean CheckParames(Map<String,String> map){
        if (String.valueOf(map.get("id")) == null || String.valueOf(map.get("email")) == null) {
            return false;
        }
        return true;
    }
}
